package com.cskaoyan.market.controller;

import com.cskaoyan.market.bo.AddGoodsBo;
import com.cskaoyan.market.bo.MarketUpdateGoodsBo;
import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.service.MarketGoodsService;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.MarketGoodsDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Author 鲸落 功能正常
 * @Date 2024/5/7 9:39
 * @Version 1.0
 */
@RestController
@RequestMapping("admin/goods")
public class AdminGoodsController {
    @Autowired
    private MarketGoodsService goodsService;

    //这个接口我们需要做的事情便是返回品牌和商品分类的信息，商品的分类需要注意有一二级分类
    @GetMapping("catAndBrand")
    public Object catAndBrand(HttpServletRequest req, HttpServletResponse resp) {
        Map<String, Object> data = goodsService.catAndBrand();
        return ResponseUtil.ok(data);
    }

    @PostMapping("update")
    public Object update(@RequestBody MarketUpdateGoodsBo marketUpdateGoodsBo) {
        goodsService.update(marketUpdateGoodsBo.getAttributes(),marketUpdateGoodsBo.getGoods(),marketUpdateGoodsBo.getProducts(),marketUpdateGoodsBo.getSpecifications());
        return ResponseUtil.ok(null);
    }

    @GetMapping("detail")
    public Object detail(Integer id) {
        MarketGoodsDetailVo goodsDetailVoList = goodsService.detail(id);
        return ResponseUtil.ok(goodsDetailVoList);
    }

    @GetMapping("list")
    public Object list(String page, String limit, String goodsSn, String name, String sort, String order, String goodsId) {
        Integer limitParam = null;
        Integer pageParam = null;
        try {
            limitParam = Integer.parseInt(limit);
            pageParam = Integer.parseInt(page);
        } catch (Exception e) {
            //直接返回一个响应结果
            return ResponseUtil.badArgument();
        }
        List<MarketGoods> goodsList = goodsService.list(pageParam, limitParam, sort, order, goodsId, goodsSn, name);
        return ResponseUtil.okList(goodsList);
    }

    @PostMapping("create")
    public Object create(@RequestBody AddGoodsBo addGoodsBo) {
        //通过注解获取位于请求体里面的json字符串
        //商品上架的逻辑：将这些数据存储到几张表中
        goodsService.create(addGoodsBo.getGoods(), addGoodsBo.getSpecifications(), addGoodsBo.getProducts(), addGoodsBo.getAttributes());
        return ResponseUtil.ok();
    }

    @PostMapping("delete")
    public Object delete(@RequestBody MarketGoods goods) {
        goodsService.delete(goods.getId());
        return ResponseUtil.ok();
    }
}
